<?	

$_values = array('id', 'id_index', 'attr', 'assoc', 'name', 'value', 'text', 'tag');

$_settings = $e['root']->getElementsByTagName('settings');
$_data = $e['root']->getElementsByTagName('content');
$_cpn = $DATA['component'];

if ( $_settings->length || $_data->length) {

	if (is_file("$_cpn[path]/admin/cleanup/init.php")) {
		include "$_cpn[path]/admin/cleanup/init.php";
	}
		
	if ($_settings->length) {
		$_settings = $_settings->item(0);

		if (($temp = $_settings->getElementsByTagName('set')) && $temp->length) {
			
			$d = array();
			$s = array();
			
			foreach($temp as $set) {
				$rec = array('id_data' => D_MB_SETTINGS, 'id_parent' => $_cpn['id']);

				foreach($_values as $key) $rec[$key] = $set->getAttribute($key);
				unset($rec['id']);

				$d[] = $rec;
				$s[$rec['name']] = $rec['value'];
			}
			
			dataset('db_insert', tb('data'), $d);	// store all settings
		}


	}
	
	$_path = FP_UPLOAD_PATH.$s['path'].'/';
	if (!is_dir($_SERVER['DOCUMENT_ROOT'].$_path)) @mkdir($_SERVER['DOCUMENT_ROOT'].$_path);
	
	
	
	if ($_data->length) {	// upload data
		$_data = $_data->item(0);

		if (($temp = $_data->getElementsByTagName('data')) && $temp->length) {
			
			foreach($temp as $set) {
				$rec = array('id_data' => D_MB_DATA, 'id_parent' => $_cpn['id']);

				foreach($_values as $key) $rec[$key] = $set->getAttribute($key);
				$id_old = $rec['id'];
				unset($rec['id']);
				
				
				$_data_id = datafetch('db_insert', tb('data'), $rec);	// get the new data ID
				



				if ($ft = $set->getAttribute('image')) {
					$oldname = str_replace('<id>', $id_old, $ft);
					$newname = explode('/', str_replace('<id>', $_data_id, $ft));
				
					if (($f = @zip_read($zip)) && (@zip_entry_name($f) == $oldname) && @zip_entry_open($zip, $f, "r")) {
						$buf = @zip_entry_read($f, @zip_entry_filesize($f));
						@zip_entry_close($f);
						
						$fn = array_pop($newname);
						
						$p = '';
						if (sizeof($newname)) {
							foreach($newname as $dir) {
								if (!is_dir($_SERVER['DOCUMENT_ROOT'].$_path.($p .= $dir)))
									@mkdir($_SERVER['DOCUMENT_ROOT'].$_path.$p);
								$p .= '/';
							}
						}
						
//debug(array('old' => $oldname, 'new p' => $p, 'new f' => $fn));

						file_put_contents($_SERVER['DOCUMENT_ROOT'].$_path.$p.$fn, $buf);
						
					} else {
						diags('<p class="error">Беда с файлами в архиве</p>');
						break;
					}
					
				}



				
				if (($m = $set->getElementsByTagName('meta')) && $m->length) {	// upload meta

					$d = array();
		
					foreach($m as $me) {
					
						$rec = array('id_data' => D_MB_META, 'id_parent' => $_data_id);
						foreach($_values as $key) $rec[$key] = $me->getAttribute($key);
						unset($rec['id']);

						$d[] = $rec;
					}
					
//debug($d, "meta of $_data_id");
					dataset('db_insert', tb('data'), $d);	// store all metas for the current data
				}
			}

		}
		
	}


return '<p class="success">'.t('Component data was loaded successfully', 'cpn').'</p>';

}